package com.zhengsr.latte.ec.ec.main.refresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.zhengsr.latte.app.Latte;
import com.zhengsr.latte.delegates.LatteDelegate;
import com.zhengsr.latte.ec.ec.main.recyclerview.ItemClickListener;
import com.zhengsr.latte.ec.ec.main.recyclerview.MultiAdapter;
import com.zhengsr.latte.net.RestClient;
import com.zhengsr.latte.net.callback.RestListenerAdapter;
import com.zhengsr.latte.ui.recycler.DataConverter;
import com.zhengsr.latte.ui.recycler.MultipleBean;

import java.util.List;

/**
 * Created by zhengshaorui on 2018/5/27.
 */

public class RefreshHandler implements SwipeRefreshLayout.OnRefreshListener {
    private static final String TAG = "RefreshHandler";
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private final RecyclerView RECYCLERVIEW;
    private final PagingBean BEAN;
    private final LatteDelegate PARENTDELEGATE;
    private RefreshHandler(SwipeRefreshLayout refreshLayout,RecyclerView recyclerView,
                           PagingBean bean,LatteDelegate parentDelegate){
        mSwipeRefreshLayout = refreshLayout;
        RECYCLERVIEW = recyclerView;
        BEAN = bean;
        PARENTDELEGATE = parentDelegate;
        mSwipeRefreshLayout.setOnRefreshListener(this);


    }

    public static RefreshHandler create(SwipeRefreshLayout refreshLayout,
                                        RecyclerView recyclerView,LatteDelegate parentDelegate){
        return new RefreshHandler(refreshLayout,recyclerView,new PagingBean(),parentDelegate);
    }

    private void refresh(){
        mSwipeRefreshLayout.setRefreshing(true);
        Latte.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //进行网络刷新
                mSwipeRefreshLayout.setRefreshing(false);
            }
        },3000);
    }


    public void reFreshFirstPage(){
        BEAN.setDelayed(1000);
        RestClient client = new RestClient.Builder()
                .setUrl("index")
                .setListenerAdapter(new RestListenerAdapter() {
                    @Override
                    public void onSuccess(String reposonse) {
                        super.onSuccess(reposonse);
                        JSONObject jsonObject = JSONObject.parseObject(reposonse);
                        BEAN.setTotal(jsonObject.getInteger("total"))
                                .setPageSize(jsonObject.getInteger("page_size"));
                        List<MultipleBean> lists = DataConverter.creat().getDataConver(reposonse);
                        MultiAdapter adapter = new MultiAdapter(Latte.getApplication(),lists);
                        adapter.setOnItemClickListener(ItemClickListener.create(PARENTDELEGATE));
                        RECYCLERVIEW.setAdapter(adapter);
                        //页数自加
                        BEAN.addIndex();
                    }
                })
                .builder()
                .get();

    }



    @Override
    public void onRefresh() {
        refresh();
    }
}
